﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using POS.Framework;
using BLL.Enums;
using DevExpress.Data.Filtering;
using Model.Bill;
using DevExpress.Xpo;
using BLL;
using DevExpress.Xpo.DB;

namespace POS.ModForm.Bill
{
    public partial class frmOrder : DevExpress.XtraEditors.XtraForm
    {
        public frmOrder()
        {
            InitializeComponent();
        }

        private void frmOrder_Load(object sender, EventArgs e)
        {
            using (WaitDialogForm wdf = new WaitDialogForm(MsgBox.WaitLoading))
            {
                this.gridView2.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(SpecialGridView.DrawRowIndicator);
                this.repositoryItemLookUpEditOrderType.DataSource = EnumManage.GetDictionaryEntry(typeof(OrderType));
                this.repositoryItemLookUpEditStatus.DataSource = EnumManage.GetDictionaryEntry(typeof(OrderStatus));
                SetButtonClickEvent();
                SearchData();
            }
        }
        void SetButtonClickEvent()
        {
            this.listButtonUC.IsHide(new List<string> { "btnNew" });
            this.listButtonUC.Query.Click += new EventHandler(Query_Click);
        }
        void gridView_DoubleClick(object sender, EventArgs e)
        {
            object oid = null;
            if (SpecialGridView.IsSimpleButton(sender) == false)
            {
                if (gridView2.FocusedRowHandle < 0) return;
                oid = gridView2.GetRowCellValue(gridView2.FocusedRowHandle, "OrderID");
            }
            FormFactory.OpenForm("POS.ModForm.Bill.frmOrderForm", "销售单编辑", oid, this.MdiParent);
        }
        void Query_Click(object sender, EventArgs e)
        {
            using (WaitDialogForm wdf = new WaitDialogForm(MsgBox.WaitLoading))
            {
                SearchData();
                wdf.Close();
            }
        }
        void SearchData()
        {
            GroupOperator goFilter = new GroupOperator();
            goFilter.Operands.Add(CriteriaOperator.Parse("StatusID>?", (int)OrderStatus.Editor));
            if (!string.IsNullOrEmpty(this._CreateDT.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse("CreateDT>=?", this._CreateDT.Text.Trim()));
            if (!string.IsNullOrEmpty(this._CreateDTEnd.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse("CreateDT<?", this._CreateDTEnd.Text.Trim() + " 23:58:58"));
            if (!string.IsNullOrEmpty(this._BillDate.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse("SellDate>=?", this._BillDate.Text.Trim()));
            if (!string.IsNullOrEmpty(this._BillDateEnd.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse("SellDate<?", this._BillDateEnd.Text.Trim() + " 23:58:58"));
            XPCollection<TB_Order> sd = new DevExpress.Xpo.XPCollection<TB_Order>(DatabaseSchema.Session, goFilter, new SortProperty("SellDate", SortingDirection.Descending));
            this._gcStockDetail.DataSource = sd;
            SpecialGridView.SetGridViewIndicatorWidth(sd.Count, this.gridView2);
        }
    }
}